﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace Baubixinh_client
{
    public partial class frmManageProductQty : DevExpress.XtraEditors.XtraForm
    {
        private DataTable mobjKhoHang = null;
        private DataTable mobjMauSac = null;
        private DataTable mobjSize = null;
        private int mintCurrentProductID = 0;
        private DataRow mobjRow = null;
        private DataTable mobjNhaSx = null;

        public frmManageProductQty()
        {
            InitializeComponent();
        }

        private void frmManageProductQty_Load(object sender, EventArgs e)
        {
            try
            {
                mobjKhoHang = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.STORE);
                mobjMauSac = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.COLOR);
                mobjSize = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.SIZE);
                mobjNhaSx = clsDbAccess.fncGetManufacturer(Program.gobjDb);

                lukTenKho.DataSource = mobjKhoHang;
                lukTenMau.DataSource = mobjMauSac;
                lukTensize.DataSource = mobjSize;
                txtNhaSx.Properties.DataSource = mobjNhaSx;

                treeDanhMuc.DataSource = Program.gobjDataDanhMuc;
                treeDanhMuc.ExpandAll();
                treeSP_DanhMuc.DataSource = Program.gobjDataDanhMuc;
                treeSP_DanhMuc.ExpandAll();

                txtSearch.Focus();

                dgvQty.OptionsBehavior.Editable = false;

                //disable menu
                xSetControlState(true);

                //load kho hang
                clsCommon.fncLoadRadioKhoHang(rdKhoHang);

                //hide price
                if (!Program.gblnAdminLoggedIn)
                {
                    colDon_gia.Visible = false;
                    mnuQty.Visible = false;
                    txtGiaGoc.Visible = false;
                    lblGiaGoc.Visible = false;
                }

                ////disable gia ban
                //txtGiaLe.Enabled = false;
                //txtGiaCap1.Enabled = false;
                //txtGiaGap2.Enabled = false;
                //txtGiaGap3.Enabled = false;
                //txtKhuyenMai.Enabled = false;
                //txtGiaGoc.Enabled = false;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void treeDanhMuc_GetSelectImage(object sender, DevExpress.XtraTreeList.GetSelectImageEventArgs e)
        {
            try
            {
                if (!e.FocusedNode)
                    e.NodeImageIndex = 0;
                else
                    e.NodeImageIndex = 1;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void treeDanhMuc_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                DevExpress.XtraTreeList.Nodes.TreeListNode objNode = treeDanhMuc.FocusedNode;
                if (objNode == null) return;

                //lay cat_id
                //int intCatID = clsCommon.fncCnvNullToInt(objNode.GetValue(colCategory_id_danhmuc));
                string strCatID = xGetRecusiveCatID(objNode);
                strCatID += "-1";

                DataTable objData = clsDbAccess.fncSearchSanPham(Program.gobjDb, strCatID, "", clsCommon.fncCnvNullToInt(rdHienThi.EditValue));
                xAddSelectColumn(objData);
                grdControlSanPham.DataSource = objData;
                dgvSanPham.BestFitColumns();

                objNode.ExpandAll();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private string xGetRecusiveCatID(DevExpress.XtraTreeList.Nodes.TreeListNode objNode)
        {
            string strResult = "";
            try
            {
                int intCatID = clsCommon.fncCnvNullToInt(objNode.GetValue(colCategory_id_danhmuc));

                strResult += intCatID.ToString() + ",";

                for (int i = 0; i <= objNode.Nodes.Count - 1; i++)
                {
                    strResult += xGetRecusiveCatID(objNode.Nodes[i]);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { }
            return strResult;
        }

        private bool xAddSelectColumn(DataTable objData)
        {
            bool blnResult = false;
            try
            {
                if (objData == null) return false;
                objData.Columns.Add("chon", typeof(bool));

                blnResult = true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { }
            return blnResult;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                string strSearch = txtSearch.Text.Trim();
                int intKhoID = -1;
                int intHienThi = -1;

                //if (clsCommon.fncIsBlank(strSearch)) return;
                intKhoID = clsCommon.fncCnvNullToInt(rdKhoHang.EditValue);
                intHienThi = clsCommon.fncCnvNullToInt(rdHienThi.EditValue);

                DataTable objData = clsDbAccess.fncSearchSanPham(Program.gobjDb, "", strSearch, intHienThi, intKhoID);
                xAddSelectColumn(objData);
                grdControlSanPham.DataSource = objData;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void grdControlSanPham_Click(object sender, EventArgs e)
        {
            try
            {
                int intIndex = dgvSanPham.FocusedRowHandle;
                if (intIndex < 0) return;

                int intTempID = clsCommon.fncCnvNullToInt(dgvSanPham.GetRowCellValue(intIndex, colProduct_id));
                if (intTempID == mintCurrentProductID) return;

                //neu dang sua so luong
                if (!mniEdit.Enabled)
                {
                    if (!clsCommon.fncMsgConfirm("Số lượng đang được sửa. Bạn có muốn hủy bỏ và chuyển sang sản phẩm khác?")) return;

                    //ok -> chuyển về trạng thái view
                    xSetControlState(true);
                }

                Cursor.Current = Cursors.WaitCursor;

                mintCurrentProductID = intTempID;
                DataTable objDataQty = clsDbAccess.fncGetQuantity(Program.gobjDb, mintCurrentProductID);
                DataTable objDataImportDate = clsDbAccess.fncGetNhapHangChiTietByProduct(Program.gobjDb, mintCurrentProductID);
                clsDbAccess.stProduct stPro = clsDbAccess.fncGetProductDetail(Program.gobjDb, mintCurrentProductID);
                List<int> lstCateID = clsDbAccess.fncGetProductToCategory2(Program.gobjDb, mintCurrentProductID);

                grdControlQty.DataSource = objDataQty;
                grdControlImport.DataSource = objDataImportDate;

                colTenkhoQty.GroupIndex = 1;
                colTenmauQty.GroupIndex = 2;
                dgvQty.ExpandAllGroups();

                //set gia ban
                clsDbAccess.stGiaBan stData = clsDbAccess.fncGetGiaBan2(Program.gobjDb, mintCurrentProductID);
                txtGiaLe.Text = stData.intGiaLe.ToString();
                txtGiaCap1.Text = stData.intGiaCap1.ToString();
                txtGiaCap2.Text = stData.intGiaCap2.ToString();
                txtGiaCap3.Text = stData.intGiaCap3.ToString();
                txtGiaGoc.Text = stData.intGiaGoc.ToString();
                txtKhuyenMai.Text = stData.intKhuyenMai.ToString();

                txtMaSP.Text = clsCommon.fncCnvNullToString(dgvSanPham.GetRowCellValue(intIndex, colProduct_ean));
                txtTenSP.Text = clsCommon.fncCnvNullToString(dgvSanPham.GetRowCellValue(intIndex, colPro_name));
                txtNhaSx.EditValue = stPro.intNhaSX;

                //set description
                txtDesc.HtmlText = stPro.strDescription;

                //set category
                treeDanhMuc.UncheckAll();
                xSetCategory(treeDanhMuc.Nodes, lstCateID);
                treeDanhMuc.Update();

                //an hien nut show - hide
                btnShow.Enabled = true;
                btnHide.Enabled = false;
                if (clsDbAccess.fncGetProductPublishStatus(Program.gobjDb, mintCurrentProductID))
                {
                    btnShow.Enabled = false;
                    btnHide.Enabled = true;
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private bool xSetControlState(bool blnState)
        {
            bool blnResult = false;
            try
            {
                mniEdit.Enabled = blnState;

                dgvQty.OptionsBehavior.Editable = !blnState;
                mniAdd.Enabled = !blnState;
                mniSave.Enabled = !blnState;
                mniCancel.Enabled = !blnState;
                mniCopy.Enabled = !blnState;
                mniPaste.Enabled = !blnState;
                mniDelete.Enabled = !blnState;

                blnResult = true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { }
            return blnResult;
        }
        
        private bool xSetCategory(DevExpress.XtraTreeList.Nodes.TreeListNodes objNodes, List<int> lstCateID)
        {
            bool blnResult = false;
            try
            {
                int intTempID;

                //lap tung node
                for (int i = 0; i <= objNodes.Count - 1; i++)
                {   //lap tung category id
                    for (int j = 0; j <= lstCateID.Count - 1; j++)
                    {
                        intTempID = clsCommon.fncCnvNullToInt(objNodes[i].GetValue(colIdDanhMuc));
                        if (intTempID == lstCateID[j])
                        {
                            objNodes[i].Checked = true;                            
                        }
                    }
                    if (objNodes[i].HasChildren) xSetCategory(objNodes[i].Nodes, lstCateID);
                }

                return true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { }
            return blnResult;
        }

        private void mniEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                xSetControlState(false);
                dgvQty.ClearGrouping();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                dgvQty.AddNewRow();
                dgvQty.SetFocusedRowCellValue(colProductIdQty, mintCurrentProductID);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ không được lưu. Bạn có chắc muốn hủy?")) return;

                grdControlSanPham_Click(null, null);
                xSetControlState(true);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            bool blnSuccess = true;

            try
            {
                clsDbAccess.stSoLuong stData;
                int intTongSoLuong = 0;

                if (!xCheckValid())
                {
                    clsCommon.fncMsgError("Dữ liệu bị trùng lặp. Hãy kiểm tra lại.");
                    return;
                }

                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ được lưu, bạn có chắc chắn?")) return;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();
                stData = new clsDbAccess.stSoLuong();
                try
                {
                    clsDbAccess.fncDeleteQuantity(Program.gobjDb, Program.gobjTrans, mintCurrentProductID);

                    for (int i = 0; i <= dgvQty.RowCount - 1; i++)
                    {
                        stData.intProductId = mintCurrentProductID;
                        stData.intKhoId = clsCommon.fncCnvNullToInt(dgvQty.GetRowCellValue(i, colTenkhoQty));
                        stData.intMauId = clsCommon.fncCnvNullToInt(dgvQty.GetRowCellValue(i, colTenmauQty));
                        stData.intSizeId = clsCommon.fncCnvNullToInt(dgvQty.GetRowCellValue(i, colTensizeQty));
                        stData.intSoluong = clsCommon.fncCnvNullToInt(dgvQty.GetRowCellValue(i, colSoluongQty));
                        stData.intKhoiluong = clsCommon.fncCnvNullToInt(dgvQty.GetRowCellValue(i, colKhoiluongQty));

                        intTongSoLuong += stData.intSoluong;

                        blnSuccess = blnSuccess && clsDbAccess.fncInsertQuantity(Program.gobjDb, Program.gobjTrans, stData);
                    }

                    //update tổng số lượng
                    blnSuccess = blnSuccess && clsDbAccess.fncUpdateProductQuantity(Program.gobjDb, Program.gobjTrans, mintCurrentProductID, intTongSoLuong);

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    clsCommon.fncMsgInfo("Lưu dữ liệu thành công.");

                    xSetControlState(true);
                    colTenkhoQty.GroupIndex = 1;
                    colTenmauQty.GroupIndex = 2;
                    dgvSanPham.SetFocusedRowCellValue(colProduct_quantity, intTongSoLuong);
                    dgvQty.ExpandAllGroups();
                }
                else
                {
                    clsCommon.fncMsgError("Lưu dữ liệu thất bại. Xin hãy thử lại.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void dgvQty_KeyDown(object sender, KeyEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (e.Control && e.KeyCode == Keys.C)
                {
                    //neu khong ở trạng thái sửa thì thoát
                    if (mniEdit.Enabled) return;

                    //copy row
                    mobjRow = dgvQty.GetFocusedDataRow();
                }

                if (e.Control && e.KeyCode == Keys.V)
                {
                    //neu khong ở trạng thái sửa thì thoát
                    if (mniEdit.Enabled) return;

                    if (mobjRow == null) return;

                    //paste row
                    DataTable objTempTable = (DataTable)grdControlQty.DataSource;
                    objTempTable.Rows.Add(clsCommon.fncCopyDataRow(objTempTable, mobjRow));
                    dgvQty.FocusedRowHandle = dgvQty.RowCount - 1;
                }

                if (e.KeyCode == Keys.Delete)
                {
                    //neu khong ở trạng thái sửa thì thoát
                    if (mniEdit.Enabled) return;
                    int intIndex = dgvQty.FocusedRowHandle;
                    if (intIndex < 0) return;

                    //xóa row
                    if (!clsCommon.fncMsgConfirm("Dòng đang được chọn sẽ bị xóa. Bạn có chắc chắn?")) return;
                    dgvQty.DeleteSelectedRows();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                //copy row
                mobjRow = dgvQty.GetFocusedDataRow();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniPaste_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                if (mobjRow == null) return;

                //paste row
                DataTable objTempTable = (DataTable)grdControlQty.DataSource;
                objTempTable.Rows.Add(clsCommon.fncCopyDataRow(objTempTable, mobjRow));
                dgvQty.FocusedRowHandle = dgvQty.RowCount - 1;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                int intIndex = dgvQty.FocusedRowHandle;
                if (intIndex < 0) return;

                //xóa row
                if (!clsCommon.fncMsgConfirm("Bản ghi đang được chọn sẽ bị xóa. Bạn có chắc chắn?")) return;
                dgvQty.DeleteSelectedRows();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private bool xCheckValid()
        {
            bool blnResult = true;
            try
            {
                dgvQty.CloseEditor();
                dgvQty.UpdateCurrentRow();

                DataTable objData = (DataTable)grdControlQty.DataSource;
                string strSelect = "";
                DataRow[] objTemp = null;

                //kiem tra xem co dong nao trung lap khong
                for (int i = 0; i <= dgvQty.RowCount - 1; i++)
                {
                    int intKhoID = clsCommon.fncCnvNullToInt(dgvQty.GetRowCellValue(i, colTenkhoQty));
                    int intMauID = clsCommon.fncCnvNullToInt(dgvQty.GetRowCellValue(i, colTenmauQty));
                    int intSizeID = clsCommon.fncCnvNullToInt(dgvQty.GetRowCellValue(i, colTensizeQty));

                    strSelect = string.Format("kho_id = {0} and mau_id = {1} and size_id = {2}", intKhoID, intMauID, intSizeID);

                    objTemp = objData.Select(strSelect);

                    if (objTemp == null) continue;
                    if (objTemp.Length <= 1) continue;

                    dgvQty.FocusedRowHandle = i;
                    return false;
                }

                return true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { }
            return blnResult;
        }

        private void btnSaveGiaBan_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            bool blnSuccess = true;

            try
            {
                if (mintCurrentProductID <= 0) return;

                clsDbAccess.stGiaBan stData;
                clsDbAccess.stProduct stPro;
                int intManuID;
                string strProductEAN = "";
                string strProductName = "";

                Program.gobjTrans = Program.gobjConn.BeginTransaction();
                stData = new clsDbAccess.stGiaBan();
                stPro = new clsDbAccess.stProduct();
                try
                {
                    stData.intProductID = mintCurrentProductID;
                    stData.intGiaGoc = clsCommon.fncCnvNullToInt(txtGiaGoc.Text.Trim());
                    stData.intGiaLe = clsCommon.fncCnvNullToInt(txtGiaLe.Text.Trim());
                    stData.intGiaCap1 = clsCommon.fncCnvNullToInt(txtGiaCap1.Text.Trim());
                    stData.intGiaCap2 = clsCommon.fncCnvNullToInt(txtGiaCap2.Text.Trim());
                    stData.intGiaCap3 = clsCommon.fncCnvNullToInt(txtGiaCap3.Text.Trim());
                    stData.intKhuyenMai = clsCommon.fncCnvNullToInt(txtKhuyenMai.Text.Trim());

                    intManuID = clsCommon.fncCnvNullToInt(txtNhaSx.EditValue);
                    strProductEAN = txtMaSP.Text.Trim();
                    strProductName = txtTenSP.Text.Trim();

                    stPro.intProID = mintCurrentProductID;
                    stPro.intNhaSX = intManuID;
                    stPro.strEAN = strProductEAN;
                    stPro.strName = strProductName;
                    stPro.strDescription = txtDesc.HtmlText;

                    //update 
                    blnSuccess = blnSuccess && clsDbAccess.fncUpdateGiaBan3(Program.gobjDb, Program.gobjTrans, stData);
                    blnSuccess = blnSuccess && clsDbAccess.fncUpdateManufacturerByProduct(Program.gobjDb, Program.gobjTrans, stPro);

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    clsCommon.fncMsgInfo("Lưu dữ liệu thành công.");
                    dgvSanPham.SetFocusedRowCellValue(colPro_name, strProductName);
                    dgvSanPham.SetFocusedRowCellValue(colProduct_ean, strProductEAN);
                }
                else
                {
                    clsCommon.fncMsgError("Lưu dữ liệu thất bại. Xin hãy thử lại.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bool blnSuccess = true;

            try
            {
                int intProID = mintCurrentProductID;

                int intIndex = dgvSanPham.FocusedRowHandle;
                if (intIndex < 0) return;

                //xóa row
                if (!clsCommon.fncMsgConfirm("Sản phẩm đang được chọn sẽ bị xóa. Bạn có chắc chắn?")) return;
                
                Cursor.Current = Cursors.WaitCursor;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    //update 
                    blnSuccess = blnSuccess && clsDbAccess.fncDeleteProduct(Program.gobjDb, Program.gobjTrans, intProID);

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    clsCommon.fncMsgInfo("Xóa sản phảm thành công.");
                    dgvSanPham.DeleteRow(dgvSanPham.FocusedRowHandle);
                    if (dgvSanPham.RowCount > 0) dgvSanPham.FocusedRowHandle = 0;
                }
                else
                {
                    clsCommon.fncMsgError("Xóa sản phẩm thất bại. Xin hãy thử lại.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void btnShow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bool blnSuccess = true;

            try
            {
                int intProID = mintCurrentProductID;

                int intIndex = dgvSanPham.FocusedRowHandle;
                if (intIndex < 0) return;
                
                Cursor.Current = Cursors.WaitCursor;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    //update 
                    blnSuccess = blnSuccess && clsDbAccess.fncEnableProduct(Program.gobjDb, Program.gobjTrans, intProID);

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    btnShow.Enabled = false;
                    btnHide.Enabled = true;
                }
                else
                {
                    clsCommon.fncMsgError("Hiện sản phẩm thất bại. Xin hãy thử lại.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void btnHide_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bool blnSuccess = true;

            try
            {
                int intProID = mintCurrentProductID;

                int intIndex = dgvSanPham.FocusedRowHandle;
                if (intIndex < 0) return;

                Cursor.Current = Cursors.WaitCursor;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    //update 
                    blnSuccess = blnSuccess && clsDbAccess.fncDisableProduct(Program.gobjDb, Program.gobjTrans, intProID);

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    btnShow.Enabled = true;
                    btnHide.Enabled = false;
                }
                else
                {
                    clsCommon.fncMsgError("Ẩn sản phẩm thất bại. Xin hãy thử lại.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void btnFixErrorLastProduct_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            bool blnSuccess = true;

            try
            {
                if (mintCurrentProductID <= 0) return;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    //fix loi san pham khong co ngay thang nen bi day xuong cuoi

                    //update 
                    blnSuccess = blnSuccess && clsDbAccess.fncFixErrorLastProduct(Program.gobjDb, Program.gobjTrans, mintCurrentProductID);

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    clsCommon.fncMsgInfo("Lưu dữ liệu thành công.");
                }
                else
                {
                    clsCommon.fncMsgError("Lưu dữ liệu thất bại. Xin hãy thử lại.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }
    }
}